import login from './../../api/login'

export default {
  // 获取当前用户的信息
  getCurrentUser(context) {
    return new Promise((resolve, reject) => {
      this.dispatch('request', login.getCurrentUser()).then(res => {
        context.state.currentInfo = res.result;
        resolve(res);
      }).catch(e => {
        reject(e);
      })
    })
  },
  // 准备筛选当前租户信息状态 (先判断是否需要请求后台获取当前租户信息)； re传入参数时表示刷新currentUser数据
  toFilterUserTenants(context, re) {
    return new Promise((resolve) => {
      let currentInfo = context.state.currentInfo;
      if (currentInfo && !re) {
        context.commit('doFilterUserTenants');
        resolve();
      } else {
        context.dispatch('getCurrentUser').then((res) => {
          context.commit('doFilterUserTenants');
          resolve(res);
        })
      }
    })
  }
}

